/*-- Handle Ajax --*/
function handleAjax(url,method,data,success,beforesend,error){
   if(url != ''){
      if (typeof(method) == 'undefined'){
         method = 'POST'
      }
      if(typeof(beforesend) == 'undefined'){
         beforesend = function(){};
      }
      if(typeof(error) == 'undefined'){
         error = function(){};
      }
      if(typeof(success) == 'undefined'){
         success = function(){};
      }
      $.ajax({
         url:url,
         type:method,
         data:data,
         beforeSend: beforesend,
         success:success,
         error:error, 
         
      });  
   }
}
function toggleClassByElement(className,elm){
   if($(elm).hassClass(className)){
      $(elm).removeClass(className);
   }else{
      $(elm).addClass(className);
   }
}
function deleteOnlyRow(obj){
   id = $(obj).attr('data-id');
   data = {'id':id};
   var updateHMTL = function(data){
      if(data != ''){
         alert(data);
         window.location.reload();   
      }
      
   }
   var beforeSend = function(){}
   var conf = confirm("Xóa dòng này ?");
   if(conf)
      handleAjax('delete.php','POST',data,updateHMTL,beforeSend);
}

function activeRow(obj){
   $checkBox = $(obj).find('input[type="checkbox"].active-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var vid_id = $checkBox.attr('data-id');
   
   value = Math.abs(value-1);
   data = {'active':value,'vid_id':vid_id};
   beforeSend = function(){
      
      $checked.addClass('loading');
   };
   updateHTML = function(){
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked')
      }
      $checkBox.val(value);
      $checked.removeClass('loading');
   };
   handleAjax('ajax_active.php','POST',data,updateHTML,beforeSend);
}

function activeImage(obj){
   $checkBox = $(obj).find('input[type="checkbox"].active-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var img_id = $checkBox.attr('data-id');
   
   value = Math.abs(value-1);
   data = {'active':value,'img_id':img_id};
   beforeSend = function(){
      $checked.addClass('loading');
   };
   updateHTML = function(){
      $checkBox.val(value);
      $checked.removeClass('loading');
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked');
      }
   };
   handleAjax('ajax_active.php','POST',data,updateHTML,beforeSend);
}

/*------- Check/Uncheck hot image -------*/
function checkHotImage(obj){
   $checkBox = $(obj).find('input[type="checkbox"].hot-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var img_id = $checkBox.attr('data-id');
   
   value = Math.abs(value-1);
   data = {'hot':value,'img_id':img_id};
   beforeSend = function(){
      $checked.addClass('loading');
   };
   updateHTML = function(){
      $checkBox.val(value);
      $checked.removeClass('loading');
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked');
      }
   };
   handleAjax('ajax_check_hot.php','POST',data,updateHTML,beforeSend);
}

function activeAlbum(obj){
   $checkBox = $(obj).find('input[type="checkbox"].active-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var alb_id = $checkBox.attr('data-id');
   
   value = Math.abs(value-1);
   data = {'active':value,'alb_id':alb_id};
   beforeSend = function(){
      $checked.addClass('loading');
   };
   updateHTML = function(){
      $checkBox.val(value);
      $checked.removeClass('loading');
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked');
      }
   };
   handleAjax('ajax_active_album.php','POST',data,updateHTML,beforeSend);
}

function checkIsAvatar(obj){
   $checkBox = $(obj).find('input[type="checkbox"].active-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var img_id = $checkBox.attr('data-id');
   var alb_id = $checkBox.attr('alb-id');
   
   value = Math.abs(value-1);
   data = {'active':value,'img_id':img_id,'alb_id':alb_id};
   beforeSend = function(){
      $checked.addClass('loading');
   };
   updateHTML = function(){
      $checkBox.val(value);
      $checked.removeClass('loading');
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked');
      }
   };
   handleAjax('ajax_check_is_avatar.php','POST',data,updateHTML,beforeSend);
}


function checkHot(obj){
   $checkBox = $(obj).find('input[type="checkbox"].active-row');
   $checked  = $(obj).find('div.checker span');
   var value = $checkBox.val();
   var vid_id = $checkBox.attr('data-id');
   
   value = Math.abs(value-1);
   data = {'hot':value,'vid_id':vid_id};
   beforeSend = function(){
      $checked.addClass('loading');
   };
   updateHTML = function(){
      $checkBox.val(value);
      $checked.removeClass('loading');
      if(value == 0){
         $checked.removeClass('checked');
      }else{
         $checked.addClass('checked');
      }
   };
   handleAjax('ajax_check_hot.php','POST',data,updateHTML,beforeSend);
}
function addPerson(vid_id,per_id){
   data = {'vid_id':vid_id,'per_id':per_id};
   beforeSend = function(){
   };
   updateHTML = function(data){
   };
   handleAjax('ajax_add_person.php','POST',data,updateHTML,beforeSend);
}
function deletePerson(vid_id,per_id){
   data = {'vid_id':vid_id,'per_id':per_id};
   beforeSend = function(){
   };
   updateHTML = function(data){
   };
   handleAjax('ajax_delete_person.php','POST',data,updateHTML,beforeSend);
}
function getSelectedText(){
   if (window.getSelection) {
        return window.getSelection().toString();
    } else if (document.selection) {
        return document.selection.createRange().text;
    }
    return '';
}
function sendName(vid,name,obj){
   data = {'per_name':name,'vid':vid};
   beforeSend = function(){
      $(obj).html('Chờ...');
   };
   updateHTML = function(data){
      $(obj).html(data);
   };
   var conf = confirm("Thêm " + name + " vào danh sách ca sĩ ?");
   if(conf)
      handleAjax('quick_add_person.php','POST',data,updateHTML,beforeSend);  
}
/*------- Sử dụng để bôi đen và thêm mới ca sĩ từ list video ------------*/
function quickInsertPerson(vid,name,x,y){
   if($.trim(name) != ''){
      button = '<div title="Thêm ca sĩ" onclick="sendName('+vid+',\''+ name +'\',this)" class="quick-insert-person" style="position:absolute"> Thêm mới </div>';
      $('body').append(button);
      $('.quick-insert-person').css('left',x + 10);
      $('.quick-insert-person').css('top',y + 10);
   }else{
      $('.quick-insert-person').remove();
   }
   
}
/*------- Sử dụng để bôi đen và thêm mới ca sĩ từ trang thêm mới ca sĩ ------------*/
function quickAddPerson(obj){
   $per_name = $(obj).parent().find('#per_name');
   per_name = $per_name.val();
   if($.trim(per_name) != ""){
      data = {'per_name':per_name};
      beforeSend = function(){
      $(obj).html('Chờ...');
   };
   updateHTML = function(data){
      $(obj).html(data);
   };
   handleAjax('quick_add_person.php','POST',data,updateHTML,beforeSend);  
   }
}

/*------------- Khi focus vào input per_name --------------------*/
function perNameFocus(obj){
   $(this).val('');
   $(obj).parent().find('#add').html('Thêm nhanh');
}

/*------------- Thêm mới một ảnh vào album ---------------------*/
function addImageToAlbum(alb_id){
   if(alb_id > 0){
      var img = $('#alb-' + alb_id).val();
      if(img != ''){
         data = {'alb_id':alb_id,'link':img};
         success = function(){
            $('#alb-' + alb_id).val('');
         }
         handleAjax('ajax_add_image_album.php','POST',data,success);
      }
   }
}
/*------- Sửa tên video -------*/
function showEditVidName(obj){
   elmVidName = $(obj).parents('td').find('.edit-vid-name');
   elmVidName.removeClass('hide');
   elmVidName.focus();
}
function editQuickVidName(obj){
   val = $(obj).val();
   id = $(obj).attr('data-id');
   success = function(data){
      $(obj).addClass('hide');
      $(obj).parent().find('.vid-name').html(val);
   }
   data = {'id':id,'vid_name':val};
   handleAjax('ajax_quick_edit_name.php','POST',data,success);
}